package com.djsearch.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.djsearch.R;
import com.djsearch.adapter.SearchAppListViewAdapter;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.SearchApp;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
/**
 * 搜应用页面
 * @author chenyang2
 *
 */
public class SearchAppActivity extends BaseActivity {

	private Context mContext;
	private Button backBtn;
	private ListView appListView;
	private ImageView startSearchImg;//放大镜图片，点击开始查找
	private static int PAGE = 1;//当前显示到第几页数据
	private List<SearchApp> searchAppList;
	private List<SearchApp> nextSearchAppList;//存放下一页请求的新数据
	private EditText searchEdit;
	private SearchAppListViewAdapter searchAppListViewAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_app_layout);
		mContext = this;
		searchAppList = new ArrayList<SearchApp>();//当前页面数据
		nextSearchAppList = new ArrayList<SearchApp>(); //更多（下一页数据）
		//初始化主界面
		initView();
		//初始化主界面相关点击事件
		initOnClick();
		//初始化handler
		initHandler();
	}
	
	
	@Override
	public void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button)findViewById(R.id.btnBack);
		appListView = (ListView)findViewById(R.id.searchAppListView);
		searchAppListViewAdapter = new SearchAppListViewAdapter(mContext,searchAppList);
		appListView.setAdapter(searchAppListViewAdapter);
		startSearchImg = (ImageView)findViewById(R.id.startSearchImg);
		searchEdit = (EditText)findViewById(R.id.init_name);
		requestAPPData();
	}

	@Override
	public void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(SearchAppActivity.this, MainActivity.class, 1);
			}
		});
		startSearchImg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (searchEdit.getText().toString().equals("")) {
					startAnimation(searchEdit);
				} else {
					
					requestAPPData();
				}
				
			}
		});
	}
	/**
	 * data={"queryByName":"aaa","page":"1","pageNum":"8"}
	 * @param map
	 */
	private void requestAPPData() {
		IRequest request = new JSONReq("search");
//		request.putParam("queryByName", searchEdit.getText().toString());
		request.putParam("queryByName", "很久以前双人套餐");
		request.putParam("page", PAGE);
		request.putParam("pageNum", "8");
		showDialog("正在加载...");
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
					Toast.makeText(mContext, "数据", 5000).show();
						getDataToResolve(response);
				} else if (IResponse.Fail==result) {
					//返回数据失败
					Toast.makeText(mContext, "暂无数据", 5000).show();
				}
				missDialog();
			}
			
			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				super.onFailure(e, response);
				missDialog();
				Toast.makeText(mContext, "服务器没建好", 5000).show();
			}
			
		});
	}
	
	/**
	 * 点击返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(mContext, MainActivity.class, 1);
		}
		return false;
		
	}


	@Override
	void initHandler() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * {“result”:”0”,returnMessage:[“softName”:”爱买”,
	 * ”image’:http://aimai.jpg,”downUrl”:”http://downaimai.jsp”]}
	 */

	@Override
	void getDataToResolve(JSONResp response) {
		

		// TODO Auto-generated method stub

		JSONArray jsonArray;
		try {
			jsonArray = response.getJSONArray("returnMessage");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obJsonObject = jsonArray.getJSONObject(i);
				SearchApp searchAppBean = new SearchApp();
				searchAppBean.setSoftName(obJsonObject.getString("sorftName"));
				searchAppBean.setImage(obJsonObject.getString("image"));
				searchAppBean.setDownUrl(obJsonObject.getString("downUrl"));
				nextSearchAppList.add(searchAppBean);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (nextSearchAppList.size() == 0) {
				//当没有更多的数据了处理
			} else {
				searchAppList.addAll(nextSearchAppList);
				nextSearchAppList.clear();
			}
			searchAppListViewAdapter.refreshData(searchAppList);
		}
	
		
	}

}
